<?php

namespace common\components;

use Yii;
use yii\helpers\ArrayHelper;
use common\widgets\ueditor\UEditorAction as Action;

class UEditorAction extends Action {

    /**
     * 处理action
     */
    protected function handleAction() {
        $action = Yii::$app->request->get('action');
        switch ($action) {
            case 'config':
                $result = json_encode($this->config);
                break;

            /* 上传图片 */
            case 'uploadimage':
            /* 上传涂鸦 */
            case 'uploadscrawl':
            /* 上传视频 */
            case 'uploadvideo':
            /* 上传文件 */
            case 'uploadfile':
                $result = $this->actionUpload();
                break;

            /* 列出图片 */
            case 'listimage':
            /* 列出文件 */
            case 'listfile':
                $result = $this->actionList();
                break;

            /* 抓取远程文件 */
            case 'catchimage':
                $result = $this->actionCrawler();
                break;

            default:
                $result = json_encode([
                    'state' => '请求地址出错'
                ]);
                break;
        }
        /* 输出结果 */
        $callback = Yii::$app->request->get('callback', false);
        if ($callback !== false) {
            if (preg_match("/^[\w_]+$/", $callback)) {
                echo htmlspecialchars($callback) . '(' . $result . ')';
            } else {
                echo json_encode([
                    'state' => 'callback参数不合法'
                ]);
            }
        } else {
            $data = json_decode($result);
            if ($data && isset($data->url)) {
                $this->controller->remotePath = $data->url;
            }
            echo $result;
        }
    }

    /**
     * 上传
     * @return string
     */
    protected function actionUpload() {
        return parent::actionUpload();
        $base64 = "upload";
        $action = Yii::$app->request->get('action');
        $uploadDefaultRoot = Yii::getAlias('@uploads');
        if ($action !== 'uploadvideo') {
            return parent::actionUpload();
        }
        Yii::error($action);
        $fieldName = $this->config['videoFieldName'];
        Yii::error($fieldName);
        /* 生成上传实例对象并完成上传 */
        $file = \yii\web\UploadedFile::getInstanceByName($fieldName);
        Yii::error($file);
        $filename = $this->generateUploadName($file->extension);

        $oss = Yii::$app->alioss;
        $return = $oss->uploadFile($filename, $file->tempName);
        Yii::error($return);

        $stateInfo = 'SUCCESS';
        $url = ArrayHelper::getValue($return, "oss-request-url");
        if ($url == null) {
            $stateInfo = 'ERROR_UNKNOWN';
        } else {
            $return = $oss->putObjectACL($filename, 'public-read');
            Yii::error($return);
        }

        return json_encode([
            "state" => $stateInfo,
            "url" => $url,
            "title" => basename($filename),
            "original" => $file->name,
            "type" => $file->type,
            "size" => $file->size
        ]);
    }

    protected function generateUploadName($ext) {
        return Yii::$app->request->hostName . '/' . date('Y-m') . '/' . date('d') . '/' . md5(Yii::$app->security->generateRandomString() . $ext) . '.' . $ext;
    }

}
